package com.rongshu.view.home.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.home.contract.ArticleDetailsView;
import com.rongshu.view.home.entity.CommenOneAllDetails;
import com.rongshu.view.home.entity.CommentDetails;
import com.rongshu.view.home.entity.CommentReplysBean;
import com.rongshu.view.home.entity.ContentClass;
import com.rongshu.view.home.entity.RecomendMsg;
import com.rongshu.view.home.entity.RecommendClass;
import com.rongshu.view.home.entity.RecommendResponse;
import com.rongshu.view.home.entity.UserAttention;
import com.rongshu.view.home.entity.UserProps;
import com.rongshu.view.penpal.bean.PenpalResponse;
import com.utils.LogUtil;
import com.utils.StringUtils;
import com.widget.toast.ToastUtils;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Func1;

public class ArticleDetailsPresenter extends BasePresenter<ArticleDetailsView> {

    public ArticleDetailsPresenter(Context context) {
        super(context);
    }

    /**
     * 获取推荐语
     */
    public void getRecommendMsg(int contribute_id, int contribute_type, int pageIndex, int pageSize, String sign) {
        RetorfitService.getRecommendMsg(contribute_id, contribute_type, pageIndex, pageSize, sign)
                .compose(getView().<RecomendMsg>bindToLife())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .map(new Func1<RecomendMsg, List<RecomendMsg.ResponseBean>>() {
                    @Override
                    public List<RecomendMsg.ResponseBean> call(RecomendMsg recomendMsg) {
                        return recomendMsg.getResponse();
                    }
                })
                .subscribe(new Subscriber<List<RecomendMsg.ResponseBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<RecomendMsg.ResponseBean> responseBeanList) {
                        if (getView() != null) {
                            getView().onRecomendMsg(responseBeanList);
                        }
                    }
                });
    }

    /**
     * 获取文章详情下的评论
     */
    public void getCommentDetail(int user_id, int contribute_id, int Sort,
                                 int pageIndex, int pageSize, String sign) {
        RetorfitService.getcommentDetail
                (user_id, contribute_id, Sort,
                        pageIndex, pageSize, sign)
                .map(new Func1<CommentDetails, List<CommentDetails.ResponseBean>>() {
                    @Override
                    public List<CommentDetails.ResponseBean> call(CommentDetails commentDetails) {
                        return commentDetails.getResponse();
                    }
                })
                .compose(getView().<List<CommentDetails.ResponseBean>>bindToLife())
                .subscribe(new Subscriber<List<CommentDetails.ResponseBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<CommentDetails.ResponseBean> responseBeanList) {
                        if (getView() != null) {
                            getView().onCommentMsg(responseBeanList);
                        }
                    }
                });
    }

    /**
     * 我的所有笔友会
     */
    public void getJoinedPenpal(int user_id, int pageIndex, int pageSize, String sign) {
        RetorfitService.getJoinedPenpal(user_id, pageIndex, pageSize, sign)
                .map(new Func1<PenpalResponse, List<PenpalResponse.ResponseBean>>() {
                    @Override
                    public List<PenpalResponse.ResponseBean> call(PenpalResponse response) {
                        return response.getResponse();
                    }
                })
                .compose(getView().<List<PenpalResponse.ResponseBean>>bindToLife())
                .subscribe(new Subscriber<List<PenpalResponse.ResponseBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<PenpalResponse.ResponseBean> responseBeans) {
                        if (getView() != null) {
                            getView().onPenpalWillMsg(responseBeans);
                        }
                    }
                });
    }

    /**
     * 获取我是否关注过某一个用户
     */
    public void getIsGuanZhu(int user_id, int ta_user_id, String sign) {
        RetorfitService.getIsGuanZhu(user_id, ta_user_id, sign)
                .compose(getView().<UserAttention>bindToLife())
                .subscribe(new Subscriber<UserAttention>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(UserAttention userAttention) {
                        if (getView() != null) {
                            getView().onIsConcernWith(userAttention);
                        }
                    }
                });
    }


    /**
     * 关注
     */
    public void concernWith(int user_id, int contribute_id, int user_id_to, String sign) {
        RetorfitService.ConcernWith(user_id, contribute_id, user_id_to, sign)
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean iNetBean) {
                        if (getView() != null) {
                            getView().onConcernWith(iNetBean);
                        }
                    }
                });
    }


    /**
     * 获取用户道具卡数量
     */
    public void getUserProps(int user_id, int propsType, String sign) {
        RetorfitService.getUserProps(user_id, propsType, sign)
                .compose(getView().<UserProps>bindToLife())
                .subscribe(new Subscriber<UserProps>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(UserProps userProps) {
                        if (getView() != null) {
                            getView().onUserProps(userProps);
                        }
                    }
                });
    }

    /**
     * 读者推荐
     */
    public void postRecommend(int user_id, int contribute_id, int contribute_type, int recommend_type,
                              int pay_device, int pay_type, String sign, RecommendClass recommendClass) {
        RetorfitService.postRecommend(
                user_id, contribute_id, contribute_type, recommend_type, pay_device, pay_type, sign, recommendClass)
                .compose(getView().<RecommendResponse>bindToLife())
                .subscribe(new Subscriber<RecommendResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RecommendResponse recommendResponse) {
                        if (getView() != null) {
                            getView().onPostRecommend(recommendResponse);
                        }
                    }
                });
    }

    /**
     * 文章评论/评论回复/回复回复
     */
    public void postCommentOrReplyAdd(int user_id, int contribute_id, int comment_id,
                                      int reply_id, String sign, ContentClass contentClass) {
        RetorfitService.postCommentOrReplyAdd(user_id, contribute_id, comment_id, reply_id, sign, contentClass)
                .compose(getView().<ResponseBody>bindToLife())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody body) {
                        if (getView() != null) {
                            getView().onPostCommentOrReplyAdd(body);
                        }
                    }
                });
    }

    /**
     * 获取单个评论下的所有回复
     */
    public void getOnecommentDetail(int user_id, int comment_id, int pageIndex, int pageSize, String sign) {
        RetorfitService.getOnecommentDetail(user_id, comment_id, pageIndex, pageSize, sign)
                .map(new Func1<CommenOneAllDetails, List<CommentReplysBean>>() {
                    @Override
                    public List<CommentReplysBean> call(CommenOneAllDetails commenOneAllDetails) {
                        return commenOneAllDetails.getComment_replys();
                    }
                })
                .map(new Func1<List<CommentReplysBean>, List<CommentReplysBean>>() {
                    @Override
                    public List<CommentReplysBean> call(List<CommentReplysBean> commentReplysBeans) {
                        if (commentReplysBeans != null) {
                            for (CommentReplysBean commentReplysBean : commentReplysBeans) {
                                commentReplysBean.setAvatar(commentReplysBean.getFrom_avatar());
                                commentReplysBean.setVip(commentReplysBean.isFrom_vip());
                            }
                        }
                        return commentReplysBeans;
                    }
                })
                .compose(getView().<List<CommentReplysBean>>bindToLife())
                .subscribe(new Subscriber<List<CommentReplysBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<CommentReplysBean> commentReplysBeans) {
                        if (getView() != null) {
                            getView().onOnecommentDetail(commentReplysBeans);
                        }
                    }
                });
    }

    /**
     * 评论点赞（任何评论）
     */
    public void getCommentlike(int user_id, int type, int obj_id, int comment_id, String sign) {
        RetorfitService.getCommentlike(user_id, type, obj_id, comment_id, sign)
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean iNetBean) {
                        if (getView() != null) {
                            getView().onCommentlike(iNetBean);
                        }
                    }
                });
    }


    /**
     * 处理文章内容
     *
     * @param html
     */
    public void dealWithContent(final String html) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) {
                List<String> textList = StringUtils.cutStringByImgTag(html);
                for (int i = 0; i < textList.size(); i++) {
                    String text = textList.get(i);
                    emitter.onNext(text);
                }
            }
        }).subscribeOn(Schedulers.io())//生产事件在io
                .observeOn(AndroidSchedulers.mainThread())//消费事件在UI线程
                .subscribe(new Observer<String>() {
                    @Override
                    public void onComplete() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.show("解析错误：图片不存在或已损坏");
                        LogUtil.e("onError: " + e.getMessage());
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        if (isViewAttached())
                            getView().onRxjavaSubscribe(d);
                    }

                    @Override
                    public void onNext(String text) {
                        if (isViewAttached())
                            getView().onCallBackDealWithContent(text);
                    }
                });
    }

    /**
     * 文章评论删除/回复删除
     */
    public void getCommentOrReplyDelete(int user_id, int contribute_id, int comment_id, int reply_id, String sign) {
        RetorfitService.getCommentOrReplyDelete(user_id, contribute_id, comment_id, reply_id, sign)
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {
                        if (getView() != null) {
                            getView().onCommentOrReplyDelete(bean);
                        }
                    }
                });
    }
}